/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.kipme.connectors;

import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.kipme.entities.Departmentdescription;
import org.kipme.entities.Magazinedproducts;
import org.kipme.entities.Orderedproducts;
import org.kipme.entities.Orders;

/**
 *
 * @author Domyślny
 */
public class DatabaseConnector {
    private static Session session;
    
    private static void prepareSession(){
        session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
    }
    
    private static void commitSession(){
        session.getTransaction().commit();
    }
    
    ///////////////////
    /////// ADD ///////
    ///////////////////
    
    public static void AddEntity(Object object) {
        prepareSession();
        session.save(object);
        commitSession();
    }
    
    public static void AddEntities(List<Object> objects) {
        prepareSession();
        for (int i = 0; i < objects.size(); i++) {
            session.save(objects.get(i));
        }
        commitSession();
    }
    
    ///////////////////
    /////// GET ///////
    ///////////////////
    
    public static Magazinedproducts GetProductById(int productId) {
        Magazinedproducts ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Magazinedproducts where id = " + productId);
        if(q.list().size() > 0){
            ret = (Magazinedproducts) q.list().get(0);
        }        
        
        commitSession();        
        return ret;
    }

    public static List<Magazinedproducts> GetAllProducts() {
        List<Magazinedproducts> ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Magazinedproducts");
        if(q.list().size() > 0){
            ret = (List<Magazinedproducts>) q.list();
        }
        commitSession();
        
        return ret;
    }

    public static Magazinedproducts GetProductByProductCode(String productCode) {
        Magazinedproducts ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Magazinedproducts where productCode = " +  "'" + productCode + "'");
        if(q.list().size() > 0)
        {
            ret = (Magazinedproducts) q.list().get(0);
        }
        commitSession();
        return ret;        
    }

    public static List<Magazinedproducts> GetProductsByTyp(String typ) {
        List<Magazinedproducts> ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Magazinedproducts where type =" + "'" + typ + "'");
        if(q.list().size() > 0){
            ret = (List<Magazinedproducts>) q.list();
        }
        commitSession();
        return ret;
    }

    public static List<Magazinedproducts> GetProductsByCategory(String kategoria) {
        List<Magazinedproducts> ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Magazinedproducts where category =" + "'" + kategoria + "'");
        if(q.list().size() > 0){
            ret = (List<Magazinedproducts>) q.list();
        }
        commitSession();
        return ret;
    }

    public static List<Magazinedproducts> GetProductsByTypAndCategory(String typ, String category) {
        List<Magazinedproducts> ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Magazinedproducts where type = " + "'" + typ + "' and category = '" + category + "'");
        if(q.list().size() > 0){
            ret = (List<Magazinedproducts>) q.list();
        }
        commitSession();
        return ret;
    }

    public static List<org.kipme.entities.Orders> GetAllOrders() {
        List<org.kipme.entities.Orders> ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Orders");
        if(q.list().size() > 0){
            ret = (List<org.kipme.entities.Orders>) q.list();
        }
        
        commitSession();
        return ret;
    }

    public static List<Orders> GetOrdersByDate(Date startDate, Date endDate) {
        List<Orders> ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Orders");
        if(q.list().size() > 0){
            ret = (List<Orders>) q.list();
        }
        
        for (int i = 0; i < ret.size(); i++) {
            if(ret.get(i).getDate().after(startDate) && ret.get(i).getDate().before(endDate)){
                ret.remove(i);
            }
        }
        commitSession();
        
        return ret;
    }

    public static List<Orderedproducts> GetOrderedProducts(int orderId) {
        List<Orderedproducts> ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Orderedproducts where orderid = " + orderId);
        if(q.list().size() > 0){
            ret = (List<Orderedproducts>)q.list();
        }
        commitSession();
        return ret;
    }
    
    public static Orderedproducts GetOrderedProductByCode(int orderId, String productCode) {
        Orderedproducts ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Orderedproducts where orderid = " + orderId + " and productcode = '" + productCode + "'");
        if(q.list().size() > 0){
            ret = (Orderedproducts)q.list().get(0);
        }
        commitSession();
        return ret;
    }

    public static Orders GetOrdersByDepartmentOrderId(String departmentOrderId) {
        Orders ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Orders where departmentorderid = '" + departmentOrderId + "'");
        if(q.list().size() > 0){
            ret = (Orders) q.list().get(0);
        }
        
        commitSession();
        
        return ret;
    }

    public static Departmentdescription GetDepartmentById(int id) {
        Departmentdescription ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Departmentdescription where id = " + id);
        if(q.list().size() > 0){
            ret = (Departmentdescription) q.list().get(0);
        }
        
        commitSession();
        return ret;
    }

    public static List<Departmentdescription> GetAllDepartments() {
        List<Departmentdescription> ret = null;
        
        prepareSession();
        Query q = session.createQuery("from Departmentdescription");
        if(q.list().size() > 0){
            ret = (List<Departmentdescription>) q.list();
        }
        commitSession();
        
        return ret;
    }
    
    ///////////////////
    ////// REMOVE /////
    ///////////////////
    
    public static void RemoveEntity(Object object) {
        prepareSession();
        session.delete(object);
        commitSession();
    }
    
    ///////////////////
    /////// EDIT //////
    ///////////////////

    public static void EditEntity(Object object) {
        prepareSession();
        session.update(object);
        commitSession();
    }

    ///////////////////
    ////// CHECK //////
    ///////////////////
    
    public static boolean CheckIfProductExistsByProductCode(String productId) {
        if(GetProductByProductCode(productId) != null){
            return true;
        }
        
        return false;
    }

}
